var listPage = avalon.define({
	$id: "listPage",
	products: [],
	noProductData: "",
	p_pageNo: 1,
	noSolutionData: "",
	s_pageNo: 1,
	solutions: [],
	flag: ""
});

mui.ready(function() {
	
	
	$("#search").focus();
	var mui_product = mui('#product-list');
	var mui_solution = mui('#solution-list');
	var q = $("#search").val();
	
	if(""==q){
		listPage.flag="";
	}else{
		listPage.flag="1";
	}

	var mui_product = mui('#product-list');
	mui_product.pullRefresh({
		container: "#product-list",
		up: {
			height: 50,
			auto: true,
			contentdown: $.i18n.prop('contentDown'),
			contentover: $.i18n.prop('contentOver'),
			contentrefresh: $.i18n.prop('contentRefresh'),
			contentnomore: $.i18n.prop('noData'),
			callback: function() {
				setTimeout(function() {
					mui.ajax('/search.do', {
						data: {
							q: $("#search").val(),
							type: 1,
							pageNo: listPage.p_pageNo
						},
						dataType: 'json',
						success: function(items) {
							var products = listPage.products;
							
							avalon.each(items, function(i, item) {
								products.push(item);
							});
							listPage.products = products;
							mui_product.pullRefresh().endPullupToRefresh(false);
							if(listPage.products.length == 0) {
								listPage.noProductData = "<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
							}
							if(items.length > 0) {
								listPage.p_pageNo = listPage.p_pageNo + 1;
							}

						}
					});
                           
				}, window.DAHUA_CONFIG.LOADING_TIME);

			}
		}

	});

	var mui_solution = mui('#solution-list');
	mui_solution.pullRefresh({
		container: "#solution-list",
		up: {
			height: 50,
			auto: true,
			contentdown: $.i18n.prop('contentDown'),
			contentover: $.i18n.prop('contentOver'),
			contentrefresh: $.i18n.prop('contentRefresh'),
			contentnomore: $.i18n.prop('noData'),
			callback: function() {
				listPage.noSolutionData = "";
				setTimeout(function() {
					var q = $("#search").val();
					mui.ajax('/search.do', {
						data: {
							q: $("#search").val(),
							type: 2,
							pageNo: listPage.s_pageNo
						},
						dataType: 'json',
						success: function(items) {
							var solutions = listPage.solutions;
							avalon.each(items, function(i, item) {
								solutions.push(item);
							});
							listPage.solutions = solutions;
							mui_solution.pullRefresh().endPullupToRefresh(false);
							if(listPage.solutions.length == 0) {
								listPage.noSolutionData = "<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
							}
							if(items.length > 0) {
								listPage.s_pageNo = listPage.s_pageNo + 1;
							}

						}
					});

				}, window.DAHUA_CONFIG.LOADING_TIME);

			}
		}

	});

	mui("#product-list").on('tap', 'li', function() {
		var pid = this.getAttribute('id');
		var url = this.getAttribute('href');
		mui.openWindow({
			url: url + "?id=" + pid
		});
	});

	mui("#solution-list").on('tap', 'a', function() {
		var sid = this.getAttribute('id');
		var url = this.getAttribute('href');
		mui.openWindow({
			url: url + "?sid=" + sid
		});
	});

	document.getElementById("search").addEventListener('keyup', function(event) {
		var e = event || window.event || arguments.callee.caller.arguments[0];
		if(e && e.keyCode == 13) {
			listPage.flag = "1";
			listPage.noSolutionData = "";
			listPage.noProductData = "";
			var q =$("#search").val();
			if(""==q){
				listPage.flag="";
				listPage.products=[];
				listPage.solutions = [];
				mui.toast("Please enter keyword");
				$("#search").focus();
			}else{
				mui.ajax('/search.do', {
					data: {
						q: q,
						type: 1,
						pageNo: 1
					},
					dataType: 'json',
					success: function(items) {
						if(items.length == 0) {
							listPage.noProductData = "<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
							listPage.products=[];
						} else {
							listPage.products = items;
							listPage.p_pageNo = listPage.p_pageNo + 1;
						}
					}
				});
				mui.ajax('/search.do', {
					data: {
						q: q,
						type: 2,
						pageNo: 1
					},
					dataType: 'json',
					success: function(items) {
						if(items.length == 0) {
							listPage.noSolutionData = "<img src='../../resources/images/noDataImg.png'><p>"+$.i18n.prop('No_data')+"</p>";
							listPage.solutions = [];
						} else {
							listPage.solutions = items;
							listPage.s_pageNo = listPage.s_pageNo + 1;
						}
					}
				});
				
			}
			
			
		}
	});

});
